//file:noinspection ALL
pipeline {
    agent any

    environment {
        projectName = 'clever-app-task-01'
        // 源码配置
        git = 'https://github.com/Lzw2016/clever-examples.git'
        gitCredentials = 'github_lizw'
        // 构建配置
        baseDir = "${WORKSPACE}"
        profiles = 'task-01'
        port = "31001"
        JAVA_MEM_OPTS = " -DappName=${projectName} -server -Xmx1g -Xms1g"
        DATABASE_OPTS = " -Ddatabase.codeset=ISO-8859-1 -Ddatabase.logging=false"
        JAVA_OPTS_EXT = " -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dapplication.codeset=UTF-8 -Dfile.encoding=UTF-8 -Duser.timezone=Asia/Shanghai --add-opens java.base/java.lang=ALL-UNNAMED"
        // 自定义环境变量
        JAVA_HOME = '/opt/jenkins-agent/env/java/jdk-17.0.9'
        GRADLE_HOME = '/opt/jenkins-agent/env/gradle/gradle-6.8.3'
        MAVEN_HOME = '/opt/jenkins-agent/env/maven/apache-maven-3.9.6'
        NODE_HOME = '/opt/jenkins-agent/env/nodejs/node-v20.10.0-linux-x64'
        PATH = "$JAVA_HOME/bin:$GRADLE_HOME/bin:$MAVEN_HOME/bin:$NODE_HOME/bin:$PATH"
    }

    stages {
        stage('#检查环境') {
            steps {
                sh """
                    java -version
                """
            }
        }

        stage('#拉取代码') {
            steps {
                checkout([
                        $class           : 'GitSCM',
                        branches         : [[name: '*/main']],
                        userRemoteConfigs: [[url: "${git}", credentialsId: "${gitCredentials}"]],
                        extensions       : [],
                ])
            }
        }

        stage('#编译项目') {
            steps {
                sh """
                    cd $baseDir
                    chmod +x gradlew
                    ./gradlew :clever-task-example:build -x test
                """
            }
        }

        stage('#部署服务') {
            steps {
                sh """
                    pid=`ps -ef | grep "DappName=${projectName}" | grep -v 'grep' | awk '{print \$2}'`
                    if [ -z \$pid ];then
                        echo '${projectName}未运行'
                    else
                        ps -ef | grep "DappName=${projectName}" | grep -v 'grep' | awk '{print \$2}' | xargs kill
                        echo '${projectName}已停止!'
                        sleep 3s
                        pid=`ps -ef | grep "DappName=${projectName}" | grep -v 'grep' | awk '{print \$2}'`
                        if [ -n "\$pid" ];then
                            ps -ef | grep "DappName=${projectName}" | grep -v 'grep' | awk '{print \$2}' | xargs kill -9
                            echo '${projectName}已停止(强制)!'
                            sleep 1s
                        fi
                    fi
    
                    cd $baseDir/clever-task-example/build/libs/
                    rm -rf ../runtime
                    cp -r ./ ../runtime
                """
                withEnv(['JENKINS_NODE_COOKIE=dontkillme']) {
                    sh """
                        cd $baseDir/clever-task-example/build/libs/../runtime/
                        java $JAVA_MEM_OPTS $DATABASE_OPTS $JAVA_OPTS_EXT -jar ./clever-task-example-0.0.1-SNAPSHOT.jar --spring.profiles.active=global,${profiles} --web.port=${port} >>/dev/null 2>&1 &
                        echo "查看服务日志: tail -F /data/logs/clever-app-task-01/clever-app-task-01.log -n 100"
                    """
                }
            }
        }
    }
}
